package com.example.pyphone3;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;

public class PermissionUtils {
    //先定义
    private static final int REQUEST_EXTERNAL_STORAGE = 1;

    private static String[] PERMISSIONS_STORAGE = {
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE"};


    private static String[] PERMISSIONS_CAMERA_AND_STORAGE = {
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,
            Manifest.permission.INTERNET,
            Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.READ_PHONE_NUMBERS,
            Manifest.permission.ACCESS_WIFI_STATE,
            Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS};

    /**
     *
     *     <uses-permission android:name="android.permission.INTERNET"></uses-permission>
     *     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
     *     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
     *     <uses-permission android:name="android.permission.READ_PHONE_NUMBERS" />
     *     <uses-permission
     *         android:name="android.permission.READ_PRIVILEGED_PHONE_STATE"
     *         tools:ignore="ProtectedPermissions" />
     *     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
     *     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
     *     <uses-permission
     *         android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
     *         tools:ignore="ProtectedPermissions"></uses-permission>
     * 解决安卓6.0以上版本不能读取外部存储权限的问题
     *
     * @param activity
     * @param requestCode
     */
    public static boolean isGrantExternalRW(Activity activity, int requestCode) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            int WRITE_EXTERNAL_STORAGE = activity.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            int CAMERA = activity.checkSelfPermission(Manifest.permission.CAMERA);
            int INTERNET = activity.checkSelfPermission(Manifest.permission.INTERNET);
            int ACCESS_NETWORK_STATE = activity.checkSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE);
            int CALL_PHONE = activity.checkSelfPermission(Manifest.permission.CALL_PHONE);
            int READ_PHONE_STATE = activity.checkSelfPermission(Manifest.permission.READ_PHONE_STATE);
            int READ_EXTERNAL_STORAGE = activity.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
            int READ_PHONE_NUMBERS = activity.checkSelfPermission(Manifest.permission.READ_PHONE_NUMBERS);
            int ACCESS_WIFI_STATE = activity.checkSelfPermission(Manifest.permission.ACCESS_WIFI_STATE);
            int MOUNT_UNMOUNT_FILESYSTEMS = activity.checkSelfPermission(Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
            int READ_SMS = activity.checkSelfPermission(Manifest.permission.READ_SMS);
            int SEND_SMS = activity.checkSelfPermission(Manifest.permission.SEND_SMS);
            int RECEIVE_SMS = activity.checkSelfPermission(Manifest.permission.RECEIVE_SMS);
            int READ_CONTACTS = activity.checkSelfPermission(Manifest.permission.READ_CONTACTS);
            int RECEIVE_WAP_PUSH = activity.checkSelfPermission(Manifest.permission.RECEIVE_WAP_PUSH);

            if (WRITE_EXTERNAL_STORAGE != PackageManager.PERMISSION_GRANTED
                    || CAMERA != PackageManager.PERMISSION_GRANTED
                    || INTERNET != PackageManager.PERMISSION_GRANTED
                    || ACCESS_NETWORK_STATE != PackageManager.PERMISSION_GRANTED
                    || READ_EXTERNAL_STORAGE != PackageManager.PERMISSION_GRANTED
                    || READ_PHONE_NUMBERS != PackageManager.PERMISSION_GRANTED
                    || ACCESS_WIFI_STATE != PackageManager.PERMISSION_GRANTED
                    || CALL_PHONE != PackageManager.PERMISSION_GRANTED
                    || READ_PHONE_STATE != PackageManager.PERMISSION_GRANTED
                    || MOUNT_UNMOUNT_FILESYSTEMS != PackageManager.PERMISSION_GRANTED
                    || READ_SMS != PackageManager.PERMISSION_GRANTED
                    || SEND_SMS != PackageManager.PERMISSION_GRANTED
                    || RECEIVE_SMS != PackageManager.PERMISSION_GRANTED
                    || READ_CONTACTS != PackageManager.PERMISSION_GRANTED
                    || RECEIVE_WAP_PUSH != PackageManager.PERMISSION_GRANTED
                    || MOUNT_UNMOUNT_FILESYSTEMS != PackageManager.PERMISSION_GRANTED
            ) {
                activity.requestPermissions(PERMISSIONS_CAMERA_AND_STORAGE, requestCode);
                return true;
            }
        }
        return true;
    }
}
